/**
 * ANDROID EMUFRAMEWORK
 * 
 * SEE LICENSE FILE FOR LICENSE INFO
 * 
 * Copyright 2011 Stephen Damm (Halsafar)
 * All rights reserved.
 * shinhalsafar@gmail.com
 */
package ca.halsafar.nesdroid;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

// Mimic the C++ side for compatibility
public enum EmulatorButtons
{
     BUTTON_INDEX_B,
     BUTTON_INDEX_A,
     BUTTON_INDEX_START,
     BUTTON_INDEX_SELECT,
     BUTTON_INDEX_REWIND,
     BUTTON_INDEX_FORWARD,
     BUTTON_INDEX_LEFT,
     BUTTON_INDEX_UP,
     BUTTON_INDEX_RIGHT,
     BUTTON_INDEX_DOWN,
     BUTTON_INDEX_COUNT;
     
     private static final String LOG_TAG = "EmulatorButtons";
     
     public static void resetInput(final Activity activity, final Context context)
     {          
          // calculate screen dimensions, max then out, @HACK - breaks if portrait
    	 float screenWidth = PreferenceFacade.getRealScreenWidth(activity, context);
    	 float screenHeight = PreferenceFacade.getRealScreenHeight(activity, context);
                 
          Log.d(LOG_TAG, "resetInput(" + screenWidth + ", " + screenHeight + ")");          
          
          float maxSize = (screenWidth > screenHeight ? screenWidth : screenHeight);

          float dpadWidth = maxSize * 0.20f;
          // tablet, reduce dpad defaults
          if (android.os.Build.VERSION.SDK_INT >= 11)
          {
         	 dpadWidth = maxSize * 0.10f;
          }
          float dpadHeight = dpadWidth;

          float dPadX = 0 + maxSize * 0.01f;
          float dPadY = screenHeight - dpadHeight - (maxSize * 0.01f);

          float buttonsWidth = maxSize * 0.085f;
          float buttonsHeight = maxSize * 0.085f;   
          float buttonSep = maxSize * 0.010f;
          
          // tablet, reduce button defaults
          if (android.os.Build.VERSION.SDK_INT >= 11)
          {
              buttonsWidth = maxSize * 0.045f;
              buttonsHeight = maxSize * 0.045f;   
              buttonSep = maxSize * 0.005f;        	 
          }
          float bButtonX = screenWidth - (buttonsWidth * 2) - buttonSep - (maxSize *0.015f);
          float bButtonY = screenHeight - buttonsHeight - (buttonsHeight / 2) - (maxSize * 0.01f);

          float aButtonX = screenWidth - buttonsWidth - (maxSize * 0.015f);
          float aButtonY = bButtonY + (buttonsHeight / 2);

          float startButtonWidth = maxSize * 0.075f;
          float startButtonHeight = maxSize * 0.04f;

          float startButtonX = (screenWidth / 2);
          float startButtonY = screenHeight - startButtonHeight - (maxSize * 0.01f);

          float selectButtonX = (screenWidth / 2) - (startButtonWidth);
          float selectButtonY = startButtonY;         
          
          float ffButtonWidth = maxSize * 0.05f;
          float ffButtonHeight = maxSize * 0.025f;
          float ffButtonX = screenWidth - (ffButtonWidth*2 + (maxSize * 0.015f));
          float ffButtonY = 0 + ffButtonHeight + (maxSize * 0.015f);
          
          float rrButtonWidth = maxSize * 0.05f;
          float rrButtonHeight = maxSize * 0.025f;
          float rrButtonX = 0 + rrButtonWidth + (maxSize * 0.015f);
          float rrButtonY = 0 + rrButtonHeight + (maxSize * 0.015f);          
          
          // default textures
          String aTex = "Textures/ButtonA.png";
          String bTex = "Textures/ButtonB.png";
          String startTex = "Textures/StartButton.png";
          String selectTex = "Textures/SelectButton.png";
          String rewindTex = "Textures/Rewind.png";
          String forwardTex = "Textures/FastForward.png";
          String dPadTex = "Textures/DirectionalPad.png";
          
          
          // default button codes - using Xperia Play
          int aCode = 23; // X
          int bCode = 99; // Square
          int startCode = 108; // Start
          int selectCode = 109; // Select
          int rewindCode = 102; // L1
          int forwardCode = 103; // R1
          int leftCode = 21;
          int upCode = 19;
          int rightCode = 22;
          int downCode = 20;
          
          InputPreferences.clearButtons(context);
          InputPreferences.setButton(context, 
                    bButtonX, bButtonY, buttonsWidth, buttonsHeight, 
                    bCode, bTex, EmulatorButtons.BUTTON_INDEX_B.ordinal());
          
          InputPreferences.setButton(context, 
                    aButtonX, aButtonY, buttonsWidth, buttonsHeight, 
                    aCode, aTex, EmulatorButtons.BUTTON_INDEX_A.ordinal());
          
          InputPreferences.setButton(context, 
                    startButtonX, startButtonY, startButtonWidth, startButtonHeight, 
                    startCode, startTex, EmulatorButtons.BUTTON_INDEX_START.ordinal());
          
          InputPreferences.setButton(context, 
                    selectButtonX, selectButtonY, startButtonWidth, startButtonHeight, 
                    selectCode, selectTex, EmulatorButtons.BUTTON_INDEX_SELECT.ordinal());
          
          InputPreferences.setButton(context, 
                    ffButtonX, ffButtonY, ffButtonWidth, ffButtonHeight, 
                    forwardCode, forwardTex, EmulatorButtons.BUTTON_INDEX_FORWARD.ordinal());
          
          InputPreferences.setButton(context, 
                    rrButtonX, rrButtonY, rrButtonWidth, rrButtonHeight, 
                    rewindCode, rewindTex, EmulatorButtons.BUTTON_INDEX_REWIND.ordinal());  
          
          // set the pseudo buttons for movements
          InputPreferences.setButton(context, 
                    0, 0, 0, 0, 
                    leftCode, null, EmulatorButtons.BUTTON_INDEX_LEFT.ordinal());          

          InputPreferences.setButton(context, 
                    0, 0, 0, 0, 
                    upCode, null, EmulatorButtons.BUTTON_INDEX_UP.ordinal());
          
          InputPreferences.setButton(context, 
                    0, 0, 0, 0, 
                    rightCode, null, EmulatorButtons.BUTTON_INDEX_RIGHT.ordinal());
          
          InputPreferences.setButton(context, 
                    0, 0, 0, 0, 
                    downCode, null, EmulatorButtons.BUTTON_INDEX_DOWN.ordinal());          
          
          InputPreferences.setAnalog(context, 
                    dPadX, dPadY, dpadWidth, dpadHeight, 
                    leftCode, upCode, rightCode, downCode,
                    dPadTex, 
                    0);      
          
          Log.d(LOG_TAG, "Done resetInput()");
     }     
}

